<?php
/**
 * Paypal payment   
 *
 * This object is used to process payments through paypal api
 *
 * @category   Paypal
 * @package    Paypal
 * @copyright  Costrategix Technologies
 * @author     Sandeep Manne<sandeep.manne@costrategix.com>
 */

class Paypal
{
	/**
	 * Use to perform paypal direct payments. It uses paypal NVP (Name Value Pairs) API
	 * For more information on paypal direct payments refer the below links
	 * https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_DoDirectPayment
	 * https://www.x.com/docs/DOC-1306
	 * 
	 * @param array $data - Refer validateDirectPaymentData for all required fields
	 * @return Zend_Http_Response
	 */
	public function doDirectPayment($data = null)
	{
		$apiCrendentials = App::getModuleConfig("paypal")->paypal->api;
		$data['USER']      = $apiCrendentials->username;
		$data['PWD']       = $apiCrendentials->password;
		$data['METHOD']    = 'DoDirectPayment';
		$data['VERSION']   = $apiCrendentials->version;
		$data['SIGNATURE'] = $apiCrendentials->signature;
		
		$this->_validateDirectPaymentData($data);
		
		$client = new Zend_Rest_Client();
		if (APPLICATION_ENV != 'production') {
			$client->setUri($apiCrendentials->endpoint->sandbox);
		} else {
			$client->setUri($apiCrendentials->endpoint->production);
		}
		//Insert Request Details to DB 
		$paypalDebugDAO = App::getDAO("paypal","paypalDebug");
		$debugId = $paypalDebugDAO->insert(array("request" => var_export($data, true)));
		$response = $client->restPost('/nvp', $data);
		
		if ($response->getStatus() == 200) {
			//Update response to DB
			$paypalDebugDAO->update(array("response" => var_export($response, true)), array('id' => $debugId));
			return $response;
		} else {
			throw new Exception("Error in processing payment ". var_export($response));
		}
	}
	
	public function createRecurringPayments($data = null)
	{
		$apiCrendentials = App::getModuleConfig("paypal")->paypal->api;
		$data['USER']      = $apiCrendentials->username;
		$data['PWD']       = $apiCrendentials->password;
		$data['METHOD']    = 'CreateRecurringPaymentsProfile';
		$data['VERSION']   = $apiCrendentials->version;
		$data['SIGNATURE'] = $apiCrendentials->signature;
		
		$this->_validateDirectPaymentData($data);
		
		$client = new Zend_Rest_Client();
		if (APPLICATION_ENV != 'production') {
			$client->setUri($apiCrendentials->endpoint->sandbox);
		} else {
			$client->setUri($apiCrendentials->endpoint->production);
		}
		//Insert Request Details to DB 
		$paypalDebugDAO = App::getDAO("paypal","paypalDebug");
		$debugId = $paypalDebugDAO->insert(array("request" => var_export($data, true)));
		$response = $client->restPost('/nvp', $data);
		
		if ($response->getStatus() == 200) {
			//Update response to DB
			$paypalDebugDAO->update(array("response" => var_export($response, true)), array('id' => $debugId));
			return $response;
		} else {
			throw new Exception("Error in processing payment ". var_export($response));
		}
	}
	
	
	/**
	 * Validate direct payment data
	 * 
	 * @param $data
	 * @return boolean
	 */
	private function _validateDirectPaymentData($data)
	{
		$cardTypes = array('Visa','MasterCard','Discover','Amex');
		if (!($data['IPADDRESS'] 
				&& Zend_Validate::is($data['IPADDRESS'], 'Ip'))) {
			return false;
		}
		if (!($data['CREDITCARDTYPE'] 
				&& in_array($data['CREDITCARDTYPE'], $cardTypes)))
		{
			return false;
		}
		if (!($data['ACCT'] 
				&& Zend_Validate::is($data['ACCT'], 'CreditCard'))) {
			return false;
		}
		if (!($data['EXPDATE'] && $data['FIRSTNAME'] && $data['LASTNAME']
		   && $data['STREET'] && $data['CITY'] && $data['STATE']
		   && $data['COUNTRYCODE'] && $data['ZIP'] && $data['AMT'] 
		   && $data['INVNUM'])) {
		   		return false;
		}
		return true;
	}
}